<?php

class Product extends AppModel {

    var $alias = 'Product';
    var $name = 'Product';
    var $useTable = 'products';
    var $belongsTo = array(
        'Catproduct' => array(
            'className' => 'Catproduct',
            'foreignKey' => 'cat_id',
            'fields' => array('Catproduct.id', 'Catproduct.title')
        )
    );
    var $hasMany = array(
        'Productphoto' => array(
            'className' => 'Productphoto',
            'foreignKey' => 'product_id',
            'dependent' => true,
        )
    );

    function delete($id = null, $cascade = true) {
        App::import('Component', 'Upload');
        $this->id = $id;
        $upload = new UploadComponent();

        //delete thumbnail
        $thumbnail = $this->field('images');
        if (is_file(Configure::read('product_image_thumb_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('product_image_thumb_absolute_path') . $thumbnail);
        }
        $thumbnail = $this->field('thumbnail');
        if (is_file(Configure::read('product_image_thumb_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('product_image_thumb_absolute_path') . $thumbnail);
        }
        parent::delete($id, $cascade);
    }

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function writeHotProductsSlideshow($limit = 10) {
        $data = $this->find('all', array(
                    'conditions' => array('Product.published' => '1', 'Product.hot_product' => '1'),
                    'fields' => array('Product.title', 'Product.price', 'Product.id', 'Product.alias', 'Product.thumbnail'),
                    'order' => 'Product.id DESC',
                    'limit' => $limit,
                ));

        $html = '<div id="hot-product-slideshow">' . "\n" .
                '<ul>' . "\n";
        foreach ($data AS $row) {
            $html .= '<li>
              <p class="hinhsanpham_noibat"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . addcslashes($row['Product']['title'], '"') . '"><img src="' .
                    Configure::read('product_image_thumb_url_path') . $row['Product']['thumbnail'] .
                    '" alt="' . addcslashes($row['Product']['title'], '"') . '" /></a></p>
              <p class="tensanpham_noibat"><a title="' . addcslashes($row['Product']['title'], '"') . '" href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['title'] . '</a></p>
              <p class="giasanpham_noibat"><a title="' . addcslashes($row['Product']['title'], '"') . '" href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . number_format($row['Product']['price'], 0, '.', ',') . ' VNĐ</a></p></li>';
        }
        $html .= '
              </ul>
        ';
        $html .= '</div>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_hot_products.ctp', 'w');
        fwrite($resourse, $html);
    }

    function writeHotProductsPromotion($limit = 4) {
        $data = $this->find('all', array(
                    'conditions' => array('Product.published' => '1', 'Product.is_promotion' => '1'),
                    'fields' => array('Product.product_code', 'Product.title', 'Product.price', 'Product.price_promotion', 'Product.id', 'Product.alias', 'Product.thumbnail'),
                    'order' => 'Product.price_promotion ASC',
                    'limit' => $limit,
                ));
        $html = '<div class="spkm">';
        foreach ($data AS $row) {
            $html .= '<ul>' . "\n";
            $html .= '<li><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img alt="' . addcslashes($row['Product']['title'], '"') . '" src="' . Configure::read('product_image_thumb_url_path') . $row['Product']['thumbnail'] . '" /></a></li>';
            $html .= '<li class="tenhang1"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['title'] . '</a></li>';
            $html .= '<li class="mahang1">' . $row['Product']['product_code'] . '</li>';
            $html .= '<li class="gia1"><span style="font-size: 12px; color: rgb(102, 102, 102); text-decoration: line-through;">' . number_format($row['Product']['price'], 0, '.', ',') . ' VNĐ</span></li>';
            $html .= '<li class="giamgia">' . number_format($row['Product']['price_promotion'], 0, '.', ',') . ' VNĐ<img src="/img/khuyenmai2.png" width="17px" height="17px"/></li>';
            $html .= '<li class="ct2"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chitiet.gif" alt="Chi tiết"/></a></li>';
            $html .= '<li class="ch2"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chonhang.gif" alt="chọn hàng"/></a></li>';
            $html .= '</ul>';
        }

        $html .= '</div>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_hot_products_promotion.ctp', 'w');
        fwrite($resourse, $html);
    }

    function writeHotProductsBestbuy($limit = 8) {
        $data = $this->find('all', array(
                    'conditions' => array('Product.published' => '1'),
                    'fields' => array('Product.product_code', 'Product.title', 'Product.price', 'Product.price_promotion', 'Product.id', 'Product.alias', 'Product.thumbnail'),
                    'order' => 'Product.buy_times DESC',
                    'limit' => $limit,
                ));
        $html = '';
        $row_index = 0;
        $i = 1;
        $total_product = sizeof($data);
        $the_first  =   true;
        foreach ($data AS $row) {
            if($the_first){
                $the_first_css  =   ' style="display: block;"';
                $the_first = false;
            }else{
                $the_first_css  =   '';
            }
            if($row_index == 0){
                $html .= '<div class="product-best-buy"' . $the_first_css . '>';
            }
            $html .= '<ul class="spnb-center1">' . "\n";
            $html .= '<li class="imgs"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '"><img alt="' . $row['Product']['title'] . '" src="' . Configure::read('product_image_thumb_url_path') . $row['Product']['thumbnail'] . '" class="hinhsanpham"/></a></li>';
            $html .= '<li class="tenhang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '">' . $row['Product']['title'] . '</a></li>';
            $html .= '<li class="mahang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '">' . $row['Product']['product_code'] . '</a></li>';
            $html .= '<li class="gia"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '">' . number_format($row['Product']['price'], 0, '.', ',') . ' VNĐ</a></li>';
            $html .= '<li class="chitiet"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '"><img src="/img/chitiet.gif" alt="chi tiết" /></a></li>';
            $html .= '<li class="chonhang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html" title="' . $row['Product']['title'] . '"><img src="/img/chonhang.gif" alt="Chọn hàng" /></a></li>';
            $html .= '</ul>';
            $row_index++;
            if(($row_index == 2 ) || $i == ($total_product)){
                $html .= '</div>';
               $row_index   =   0;
            }
            $i++;
        }
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_best_buy.ctp', 'w');
        fwrite($resourse, $html);
    }

    function writeHotProductsLastest($limit = 2) {
        $data = $this->find('all', array(
                    'conditions' => array('Product.published' => '1', 'Product.hot_product' => '1'),
                    'fields' => array('Product.product_code', 'Product.title', 'Product.price', 'Product.price_promotion', 'Product.id', 'Product.alias', 'Product.thumbnail'),
                    'order' => 'Product.id DESC',
                    'limit' => $limit,
                ));
        $html = '';
        foreach ($data AS $row) {
            $html .= '<ul class="spnb-center1">' . "\n";
            $html .= '<li class="imgs"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="' . Configure::read('product_image_thumb_url_path') . $row['Product']['thumbnail'] . '" class="hinhsanpham"/></a></li>';
            $html .= '<li class="tenhang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['title'] . '</a></li>';
            $html .= '<li class="mahang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['product_code'] . '</a></li>';
            $html .= '<li class="gia"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . number_format($row['Product']['price'], 0, '.', ',') . ' VNĐ</a></li>';
            $html .= '<li class="chitiet"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chitiet.gif" /></a></li>';
            $html .= '<li class="chonhang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chonhang.gif" /></a></li>';
            $html .= '</ul>';
        }
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_product_lastest.ctp', 'w');
        fwrite($resourse, $html);
    }

    function writeHomeProductFromCategory($cat_id, $filecachename, $limit = 6) {
        $ids = array();
        $this->Catproduct->getChildAllById($cat_id, $ids);
        $ids[] = $cat_id;
        $data = $this->find('all', array(
                    'conditions' => array('Product.published' => '1', 'Product.cat_id IN (' . implode(',', $ids) . ')'),
                    'fields' => array('Product.product_code', 'Product.title', 'Product.price', 'Product.price_promotion', 'Product.id', 'Product.alias', 'Product.thumbnail'),
                    'order' => 'Product.id DESC',
                    'limit' => $limit,
                ));
        $html = '<div class="khungspnb1">';
        $row_index = 0;
        $i = 1;
        $total_product = sizeof($data);
        foreach ($data AS $row) {
            if($row_index == 0){
                $html .= '<div class="lstProduct">';
            }
            $html .= '<ul class="spnb1">' . "\n";
            $html .= '<li class="imgs"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="' . Configure::read('product_image_thumb_url_path') . $row['Product']['thumbnail'] . '" class="hinhsanpham"/></a></li>';
            $html .= '<li class="tenhang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['title'] . '</a><img src="/img/cf-1.gif"></li>';
            $html .= '<li class="mahang"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . $row['Product']['product_code'] . '</a></li>';
            $html .= '<li class="gia"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html">' . number_format($row['Product']['price'], 0, '.', ',') . ' VNĐ</a></li>';
            $html .= '<li class="ct"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chitiet.gif" /></a></li>';
            $html .= '<li class="ch"><a href="/san-pham/chi-tiet/' . $row['Product']['id'] . '-' . $row['Product']['alias'] . '.html"><img src="/img/chonhang.gif" /></a></li>';
            $html .= '</ul>';
            $row_index++;
            if(($row_index == 3 ) || $i == ($total_product)){
                $html .= '</div>';
               $row_index   =   0;
            }
            $i++;
        }
        $html .= '</div>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . "{$filecachename}.ctp", 'w');
        fwrite($resourse, $html);
    }

}

?>
